//
//  DiscoverHouseholdListViewController.m
//  HJB
//
//  Created by jiezsoft.com on 14-10-10.
//  Copyright (c) 2014年 jiezsoft.com. All rights reserved.
//

#import "DiscoverHouseholdMyCollectViewController.h"
#import "HouseholdDetailViewController.h"
#import "UIViewExt.h"
#import "CycleScrollViews.h"
#import "HttpRequest.h"
#import "UIButton+WebCache.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "DeleteNull.h"
@interface DiscoverHouseholdMyCollectViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>
@end

@implementation DiscoverHouseholdMyCollectViewController

{
    UISearchBar *search;
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
    NSMutableArray*fid;
    NSMutableArray*title;
    NSMutableArray*content;
    NSMutableArray*imageUrl;
    NSMutableArray*author;
    NSMutableArray*pubTime;
    NSMutableArray*collectCount;
    NSMutableArray*commentCount;
    NSMutableArray*LinkType;
    NSMutableArray*LinkCode;
    NSMutableArray*ImageUrl;
    CycleScrollViews *mainScorllView;
    NSString *result;
    float lastContentOffset;
    UITableView*table;
    UILabel* noLabel;
}
@synthesize alt = alt;

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.title = @"我的收藏";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
}
- (void)viewDidLoad
{
    
    [super viewDidLoad];
    [self requestData];
}
-(void)requestData{
    self.sat=nil;
    [noLabel removeFromSuperview];
    pubTime= [[NSMutableArray alloc] init];
    author= [[NSMutableArray alloc] init];
    imageUrl= [[NSMutableArray alloc] init];
    content= [[NSMutableArray alloc] init];
    title= [[NSMutableArray alloc] init];
    fid= [[NSMutableArray alloc] init];
    LinkType= [[NSMutableArray alloc] init];
    LinkCode= [[NSMutableArray alloc] init];
    ImageUrl= [[NSMutableArray alloc] init];
    
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
   
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"1" forKey:@"pageIndex"];
    [param setValue:@"25" forKey:@"pageSize"];
    
    
    NSLog(@"====%@",param);
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    
    [HttpRequest requestData:@"Discover/Household/MyCollect" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                
                arrr=[severDataDict.dataDict   objectForKey:@"DataLine"];
                
                NSString*TotalCount=[severDataDict.dataDict objectForKey:@"TotalCount"];
              
                if (TotalCount.intValue==0) {
                    
                     noLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, ScreenHeight/2-50, ScreenWidth, 17)];
                    noLabel.text=@"没有列表数据，赶紧去添加！";
                    noLabel.textColor=zhonghuiColor; noLabel.font=[UIFont systemFontOfSize:15];
                    noLabel.textAlignment=NSTextAlignmentCenter;
                    [self.view addSubview:noLabel];
                    
                }else{

                for (NSDictionary* dicc in arrr )
                {
                    
                    [author addObject:[dicc valueForKey:@"author"]];
                    [ImageUrl addObject:[Global getJudgeTheErrorStr:[dicc valueForKey:@"imageUrlFull"]]];
                    [content addObject:[dicc valueForKey:@"content"]];
                    [fid addObject:[dicc valueForKey:@"fid"]];
                    [title addObject:[dicc valueForKey:@"title"]];
                    
                    NSString* string =  [dicc valueForKey:@"pubTime"];
                    NSLog(@"-------------%@",string);
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
                    NSDate *destDate= [dateFormatter dateFromString:string];
                    
                    NSTimeInterval  timeInterval = [destDate timeIntervalSinceNow];
                    timeInterval = -timeInterval;
                    
                    
                    
                    long temp = 0;
                    
                    if (timeInterval < 60) {
                        result = [NSString stringWithFormat:@"刚刚"];
                    }
                    else if((temp = timeInterval/60) <60){
                        result = [NSString stringWithFormat:@"%ld分前",temp];
                    }
                    
                    else if((temp = temp/60) <24){
                        result = [NSString stringWithFormat:@"%ld小时前",temp];
                    }
                    
                    else if((temp = temp/24) <30){
                        result = [NSString stringWithFormat:@"%ld天前",temp];
                    }
                    
                    else if((temp = temp/30) <12){
                        result = [NSString stringWithFormat:@"%ld月前",temp];
                    }
                    else{
                        temp = temp/12;
                        result = [NSString stringWithFormat:@"%ld年前",temp];
                    }
                    [pubTime addObject:result];}
                
                [table reloadData];
                
                }
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }}
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [title count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView*imageTitle=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 90, 70 )];
    [imageTitle.layer setCornerRadius:2.0];
    [imageTitle.layer setBorderWidth:0.3];
    [imageTitle.layer setMasksToBounds:YES];
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 0.93, 0.93, 0.93, 0.93 });
    [imageTitle.layer setBorderColor:colorref2];
    [imageTitle setImageWithURL:[NSURL URLWithString:[ImageUrl objectAtIndex:indexPath.row]]placeholderImage:[UIImage imageNamed:@""]];
    [cell.contentView addSubview:imageTitle];
    NSString *str = [NSString stringWithFormat:@"%@",  [title objectAtIndex:indexPath.row]];
    UIFont *font = [UIFont systemFontOfSize:15];
    CGSize size = CGSizeMake(320, 2000);
    CGSize labelsize = [str sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
    UILabel*titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(imageTitle.right+10, 10, ScreenWidth-110,labelsize.height)];
    titleLabel.text =[NSString stringWithFormat:@"%@",  [title objectAtIndex:indexPath.row]];
    titleLabel.font=[UIFont systemFontOfSize:15];
    titleLabel.numberOfLines=0;
    [cell.contentView addSubview:titleLabel];
    UILabel*authorLabel=[[UILabel alloc]initWithFrame:CGRectMake(imageTitle.right+10, 60, ScreenWidth-110,11)];
   
    if ([[NSString stringWithFormat:@"%@",  [author objectAtIndex:indexPath.row]]length]>1) {
        if ([[NSString stringWithFormat:@"%@", [author objectAtIndex:indexPath.row] ] isEqualToString:@"<null>"]) {
            authorLabel.text =@"";
        }else{
        
         authorLabel.text =[NSString stringWithFormat:@"%@",  [author objectAtIndex:indexPath.row]];
        }
        
    }else{
    
    }
   authorLabel.font=[UIFont systemFontOfSize:11];
    [cell.contentView addSubview:authorLabel];
    UILabel*timeLabel=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-110, 60, 95,12)];
    timeLabel.text =[NSString stringWithFormat:@"%@",  [pubTime objectAtIndex:indexPath.row]];
    timeLabel.textAlignment=NSTextAlignmentRight;
    timeLabel.font=[UIFont systemFontOfSize:12];
    [cell.contentView addSubview:timeLabel];
    
    if (title.count !=indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 90-0.40, ScreenWidth-20, 0.40)];
        lineLabel1.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    
    if (title.count ==indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 90-0.40, ScreenWidth, 0.40)];
        lineLabel1.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  90;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HouseholdDetailViewController *detailViewController = [[HouseholdDetailViewController alloc] init];
    detailViewController.fid=fid[indexPath.row];
    detailViewController.titleStr=title[indexPath.row];
    detailViewController.ImageUrl=ImageUrl[indexPath.row];
    detailViewController.content=content[indexPath.row];
    [self.navigationController pushViewController:detailViewController animated:YES];
 }


- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }

}


@end
